package Views;

import Controllers.RentalCTR;
import Models.*;
import byggemarked.Tools;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JOptionPane;

/**
 * @author Morten
 */
public class RentalInfoView extends javax.swing.JFrame implements WindowListener
{
	private Rental rental;
	private RentalCTR controller = new RentalCTR();
	private Customer customerTemp = null;
	private Machine machineTemp = null;
	private CustomerPicker cp = null;
	private MachinePicker mp = null;

    public RentalInfoView(Rental rental)
	{
        initComponents();
		this.rental = rental;
		customerTF.setText(Tools.getNameOrCompanyName(this.rental.getCustomer()));
		machineTF.setText(this.rental.getMachine().getName());
		depositSP.setValue(this.rental.getDeposit());
		pricePerDaySP.setValue(this.rental.getPricePerDay());
		rentalTF.setText(Tools.makeNiceStringFromDate(this.rental.getRentalDate()));
		expectedReturnTF.setText(Tools.makeNiceStringFromDate(this.rental.getExpectedReturnDate()));
		if(this.rental.getReturnDate() != null)
		{
			returnTF.setText(Tools.makeNiceStringFromDate(this.rental.getReturnDate()));
		}
		else
		{
			returnTF.setText("Ikke afleveret endnu.");
		}
    }

	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        descriptionLabel = new javax.swing.JLabel();
        activatedCB = new javax.swing.JCheckBox();
        customerLabel = new javax.swing.JLabel();
        customerTF = new javax.swing.JTextField();
        findCustomer = new javax.swing.JButton();
        machineLabel = new javax.swing.JLabel();
        machineTF = new javax.swing.JTextField();
        findMachine = new javax.swing.JButton();
        depositLabel = new javax.swing.JLabel();
        depositSP = new javax.swing.JSpinner();
        krLabel1 = new javax.swing.JLabel();
        pricePerDayLabel = new javax.swing.JLabel();
        pricePerDaySP = new javax.swing.JSpinner();
        krLabel2 = new javax.swing.JLabel();
        rentalLabel = new javax.swing.JLabel();
        rentalTF = new javax.swing.JTextField();
        expectedReturnLabel = new javax.swing.JLabel();
        expectedReturnTF = new javax.swing.JTextField();
        returnLabel = new javax.swing.JLabel();
        returnTF = new javax.swing.JTextField();
        editButton = new javax.swing.JButton();
        returnButton = new javax.swing.JButton();
        closeButton = new javax.swing.JButton();
        findCustomer1 = new javax.swing.JButton();
        findMachine1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Udlejningsinformation");

        descriptionLabel.setText("Oversigt over det valgte udlån");

        activatedCB.setSelected(true);
        activatedCB.setText("Aktiveret");
        activatedCB.setEnabled(false);

        customerLabel.setText("Kunde:");

        customerTF.setEnabled(false);

        findCustomer.setText("Find");
        findCustomer.setEnabled(false);
        findCustomer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                findCustomerActionPerformed(evt);
            }
        });

        machineLabel.setText("Maskine:");

        machineTF.setEnabled(false);

        findMachine.setText("Find");
        findMachine.setEnabled(false);
        findMachine.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                findMachineActionPerformed(evt);
            }
        });

        depositLabel.setText("Depositum:");

        depositSP.setEnabled(false);

        krLabel1.setText("kr.");

        pricePerDayLabel.setText("Pris per dag:");

        pricePerDaySP.setEnabled(false);

        krLabel2.setText("kr.");

        rentalLabel.setText("Udlejningsdato:");

        rentalTF.setEnabled(false);

        expectedReturnLabel.setText("Forventet afleveringsdato:");

        expectedReturnTF.setEnabled(false);

        returnLabel.setText("Afleveringsdato:");

        returnTF.setEnabled(false);

        editButton.setText("Rediger");
        editButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editButtonActionPerformed(evt);
            }
        });

        returnButton.setText("Aflevér");
        returnButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                returnButtonActionPerformed(evt);
            }
        });

        closeButton.setText("Luk");
        closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeButtonActionPerformed(evt);
            }
        });

        findCustomer1.setText("Info");
        findCustomer1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                findCustomer1ActionPerformed(evt);
            }
        });

        findMachine1.setText("Info");
        findMachine1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                findMachine1ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, pricePerDayLabel)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, depositLabel))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(pricePerDaySP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 94, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(depositSP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 94, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(10, 10, 10)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(krLabel2)
                            .add(krLabel1)))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(editButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 105, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(returnButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 81, Short.MAX_VALUE)
                        .add(closeButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 105, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(descriptionLabel)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                                        .add(machineLabel)
                                        .add(5, 5, 5)
                                        .add(machineTF, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 183, Short.MAX_VALUE))
                                    .add(layout.createSequentialGroup()
                                        .add(customerLabel)
                                        .add(18, 18, 18)
                                        .add(customerTF, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 178, Short.MAX_VALUE)))
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(findCustomer)
                                    .add(findMachine))))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(findCustomer1)
                            .add(activatedCB)
                            .add(findMachine1)))
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(expectedReturnLabel)
                            .add(returnLabel)
                            .add(rentalLabel))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(rentalTF, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 230, Short.MAX_VALUE)
                            .add(expectedReturnTF, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 230, Short.MAX_VALUE)
                            .add(returnTF, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 230, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(22, 22, 22)
                        .add(descriptionLabel)
                        .add(11, 11, 11)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(customerLabel)
                            .add(customerTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(findCustomer1)
                            .add(findCustomer))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(machineLabel)
                            .add(machineTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(findMachine1)
                            .add(findMachine))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(depositLabel)
                            .add(depositSP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(krLabel1))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(pricePerDayLabel)
                            .add(pricePerDaySP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(krLabel2)))
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(activatedCB)))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(rentalLabel)
                    .add(rentalTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(expectedReturnLabel)
                    .add(expectedReturnTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(returnLabel)
                    .add(returnTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(editButton)
                    .add(returnButton)
                    .add(closeButton))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void closeButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_closeButtonActionPerformed
	{//GEN-HEADEREND:event_closeButtonActionPerformed
		this.dispose();
}//GEN-LAST:event_closeButtonActionPerformed

	private void editButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_editButtonActionPerformed
	{//GEN-HEADEREND:event_editButtonActionPerformed
		if (editButton.getText().equals("Rediger"))
		{
			findCustomer.setEnabled(true);
			findMachine.setEnabled(true);
			depositSP.setEnabled(true);
			pricePerDaySP.setEnabled(true);
			expectedReturnTF.setEnabled(true);
			expectedReturnTF.setText(Tools.makeStringFromDate(rental.getExpectedReturnDate()));
			returnTF.setEnabled(true);
			returnTF.setText(Tools.makeStringFromDate(rental.getReturnDate()));
			
			editButton.setText("Gem");
			closeButton.setText("Annuller");
		}
		else if (editButton.getText().equals("Gem"))
		{
			findCustomer.setEnabled(false);
			findMachine.setEnabled(false);
			depositSP.setEnabled(false);
			pricePerDaySP.setEnabled(false);
			expectedReturnTF.setEnabled(false);
			returnTF.setEnabled(false);

			rental.setCustomer(this.customerTemp);
			rental.setMachine(this.machineTemp);
			rental.setDeposit(Double.parseDouble(depositSP.getValue().toString()));
			rental.setPricePerDay(Double.parseDouble(pricePerDaySP.getValue().toString()));
			try
			{
				if(!expectedReturnTF.getText().equals(""))
				{
					rental.setExpectedReturnDate(Tools.makeDateFromString(expectedReturnTF.getText()));
					expectedReturnTF.setText(Tools.makeNiceStringFromDate(rental.getExpectedReturnDate()));
				}
				else
				{
					expectedReturnTF.setText(Tools.makeNiceStringFromDate(rental.getExpectedReturnDate()));
				}
			}
			catch(NumberFormatException e)
			{
				JOptionPane.showMessageDialog(this, "Den forventede afleveringsdato er skrevet i et forkert format.", "Forkert datoformat", JOptionPane.WARNING_MESSAGE);
			}

			try
			{
				if(!returnTF.getText().equals(""))
				{
					rental.setReturnDate(Tools.makeDateFromString(returnTF.getText()));
					returnTF.setText(Tools.makeNiceStringFromDate(rental.getReturnDate()));
				}
				else
				{
					rental.setReturnDate(null);
					returnTF.setText("Ikke afleveret endnu.");
				}
			}
			catch(NumberFormatException e)
			{
				JOptionPane.showMessageDialog(this, "Afleveringsdatoen er skrevet i et forkert format.", "Forkert datoformat", JOptionPane.WARNING_MESSAGE);
			}

			editButton.setText("Rediger");
			closeButton.setText("Luk");
		}
}//GEN-LAST:event_editButtonActionPerformed

	private void returnButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_returnButtonActionPerformed
	{//GEN-HEADEREND:event_returnButtonActionPerformed
		int choice = JOptionPane.showConfirmDialog(this, "Er du sikker på at du vil aflevere udlejningen?", "Bekræftelse af aflevering", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
		if(choice == 0)
		{
			this.controller.returnRental(rental);
			Double totalPrice = rental.calculateTotalPrice();
			JOptionPane.showMessageDialog(this, "Kunden skal betale: "+totalPrice+" kr.", "Betaling", JOptionPane.INFORMATION_MESSAGE);
			this.dispose();
		}
}//GEN-LAST:event_returnButtonActionPerformed

	private void findCustomerActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_findCustomerActionPerformed
	{//GEN-HEADEREND:event_findCustomerActionPerformed
		cp = new CustomerPicker();
		cp.addWindowListener(this);
		cp.setVisible(true);
	}//GEN-LAST:event_findCustomerActionPerformed

	private void findMachineActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_findMachineActionPerformed
	{//GEN-HEADEREND:event_findMachineActionPerformed
		mp = new MachinePicker();
		mp.addWindowListener(this);
		mp.setVisible(true);
	}//GEN-LAST:event_findMachineActionPerformed

	private void findCustomer1ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_findCustomer1ActionPerformed
	{//GEN-HEADEREND:event_findCustomer1ActionPerformed
		new CustomerInfoView(customerTemp).setVisible(true);
	}//GEN-LAST:event_findCustomer1ActionPerformed

	private void findMachine1ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_findMachine1ActionPerformed
	{//GEN-HEADEREND:event_findMachine1ActionPerformed
		new MachineInfoView(machineTemp).setVisible(true);
	}//GEN-LAST:event_findMachine1ActionPerformed

	public void windowClosing(WindowEvent e)
	{
		if(mp != null)
		{
			this.machineTemp = mp.getChosenMachine();
			if(this.machineTemp != null)
			{
				machineTF.setText(this.machineTemp.getName());
			}
			mp = null;
		}
		else if(cp != null)
		{
			this.customerTemp = cp.getChosenCustomer();
			if(this.customerTemp != null)
			{
				customerTF.setText(Tools.getNameOrCompanyName(customerTemp));
			}
			cp = null;
		}
	}

	public void windowOpened(WindowEvent e) {}
	public void windowClosed(WindowEvent e) {}
	public void windowIconified(WindowEvent e) {}
	public void windowDeiconified(WindowEvent e) {}
	public void windowActivated(WindowEvent e) {}
	public void windowDeactivated(WindowEvent e) {}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox activatedCB;
    private javax.swing.JButton closeButton;
    private javax.swing.JLabel customerLabel;
    private javax.swing.JTextField customerTF;
    private javax.swing.JLabel depositLabel;
    private javax.swing.JSpinner depositSP;
    private javax.swing.JLabel descriptionLabel;
    private javax.swing.JButton editButton;
    private javax.swing.JLabel expectedReturnLabel;
    private javax.swing.JTextField expectedReturnTF;
    private javax.swing.JButton findCustomer;
    private javax.swing.JButton findCustomer1;
    private javax.swing.JButton findMachine;
    private javax.swing.JButton findMachine1;
    private javax.swing.JLabel krLabel1;
    private javax.swing.JLabel krLabel2;
    private javax.swing.JLabel machineLabel;
    private javax.swing.JTextField machineTF;
    private javax.swing.JLabel pricePerDayLabel;
    private javax.swing.JSpinner pricePerDaySP;
    private javax.swing.JLabel rentalLabel;
    private javax.swing.JTextField rentalTF;
    private javax.swing.JButton returnButton;
    private javax.swing.JLabel returnLabel;
    private javax.swing.JTextField returnTF;
    // End of variables declaration//GEN-END:variables
}